//
//  GxUnLoginView.swift
//  microblog
//
//  Created by 记忆不为 on 16/9/17.
//  Copyright © 2016年 记忆不为. All rights reserved.
//

import UIKit

@objc protocol GxUnLoginViewDelegate:NSObjectProtocol{
    
    @objc optional func registerDidClick() -> Void
    
    @objc optional func loginDidClick() -> Void
}


class GxUnLoginView: UIView {
    
    // MARK: - 属性
    var follow : (() ->Void)?
    weak var delegate : GxUnLoginViewDelegate?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - view方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    // MARK: - 公用方法
    func setupInfo(title : String) -> Void {
        tipLabel.text = title
        houseImageView.image = UIImage(named: "visitordiscover_feed_image_house")
        retainUI()
    }
    func setupVisitorInfo(image : String,title : String) -> Void {
        if image != "" {
            self.houseImageView.image = UIImage(named: image)
        }else{
            self.houseImageView.isHidden = true
            self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        }
        
        tipLabel.text = title
        retainButton()
        
    }
    func rotationAnimation() -> Void {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.duration = 20
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false
        groundImageView.layer.add(animation, forKey: nil)
    }
    
    
    // MARK: - 私有方法
    private func setup() -> Void{
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        addSubview(houseImageView)
        addSubview(tipLabel)
        
        houseImageView.translatesAutoresizingMaskIntoConstraints = false
        tipLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 房子
        // centerX
        self.addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        // centerY
        self.addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: -40))
        // 消息
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: houseImageView, attribute: .centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 48))
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: -48))
    }
    
    private func retainUI() ->Void{
        groundImageView.translatesAutoresizingMaskIntoConstraints = false
        coverImageView.translatesAutoresizingMaskIntoConstraints = false
        followbutton.translatesAutoresizingMaskIntoConstraints = false
        addSubview(groundImageView)
        addSubview(coverImageView)
        addSubview(followbutton)
        self.bringSubview(toFront: self.houseImageView)
        
        // 转盘
        self.addConstraint(NSLayoutConstraint(item: groundImageView, attribute: .centerX, relatedBy: .equal, toItem: houseImageView, attribute: .centerX, multiplier: 1, constant: 0))
        // centerY
        self.addConstraint(NSLayoutConstraint(item: groundImageView, attribute: .centerY, relatedBy: .equal, toItem: houseImageView, attribute: .centerY, multiplier: 1, constant: 0))
        
        // 遮盖
        // centerX
        self.addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .centerX, relatedBy: .equal, toItem: groundImageView, attribute: .centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .centerY, relatedBy: .equal, toItem: groundImageView, attribute: .centerY, multiplier: 1, constant: 0))
        
        //关注按钮
        self.addConstraint(NSLayoutConstraint(item: followbutton, attribute: .centerX, relatedBy: .equal, toItem: groundImageView, attribute: .centerX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: followbutton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: followbutton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: followbutton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        
        //提示文本
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: groundImageView, attribute: .bottom, multiplier: 1, constant: 16))
    }
    
    
    private func retainButton() -> Void{
        registerbutton.translatesAutoresizingMaskIntoConstraints = false
        loginbutton.translatesAutoresizingMaskIntoConstraints = false
        addSubview(registerbutton)
        addSubview(loginbutton)
        //注册按钮
        self.addConstraint(NSLayoutConstraint(item: registerbutton, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: -16))
        self.addConstraint(NSLayoutConstraint(item: registerbutton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant:16))
        self.addConstraint(NSLayoutConstraint(item: registerbutton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerbutton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        //登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginbutton, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginbutton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginbutton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginbutton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 35))
        
        //提示文本
        self.addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: houseImageView, attribute: .bottom, multiplier: 1, constant: 16))
    }
    
    // MARK: - 懒加载
    
    private lazy var groundImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var coverImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var houseImageView : UIImageView = UIImageView()
    private lazy var tipLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    private lazy var followbutton : UIButton = {
        let first = UIButton()
        first.setTitleColor(UIColor.orange, for: .normal)
        self.setupButton(button : first,buttonTitle : "去关注")
        first.addTarget(self, action: #selector(clickFollowButton), for: .touchUpInside)
        return first
    }()
    private lazy var registerbutton : UIButton = {
        let second = UIButton()
        second.setTitleColor(UIColor.orange, for: .normal)
        self.setupButton(button : second,buttonTitle : "注册")
        second.addTarget(self, action: #selector(clickRegisterButton), for: .touchUpInside)
        return second
    }()
    private lazy var loginbutton : UIButton = {
        let third = UIButton()
        third.setTitleColor(UIColor.gray, for: .normal)
        self.setupButton(button : third,buttonTitle : "登录")
        third.addTarget(self, action: #selector(clickLoginButton), for: .touchUpInside)
        return third
    }()
    
    // MARK: - 快速设置按钮属性
    private func setupButton(button : UIButton,buttonTitle : String) ->Void{
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.setTitle(buttonTitle, for: .normal)
    }
    
    // MARK: - 事件方法
    @objc private func clickFollowButton() ->Void {
        follow?()
    }
    @objc private func clickRegisterButton() ->Void{
        delegate?.registerDidClick?()
    }
    @objc private func clickLoginButton() ->Void{
        delegate?.loginDidClick?()
    }
}
